package com.huan.servlet;

import com.huan.entity.News;
import com.huan.service.NewsService;
import com.huan.service.NewsServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet("/servlet/addOrUpdateNews")
public class AddOrUpdateNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建类对象
        News newsDetail = new News();
        //接收请求
        // 检查我们是否有文件上传请求
        //判断前端传过来的数据是否是 multipart 类型
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(isMultipart){
            // 为基于磁盘的文件项创建工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // 创建一个新的文件上传处理程序
            ServletFileUpload upload = new ServletFileUpload(factory);
            // 解析请求
            //获取页面普通页面组件
            List<FileItem> items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Integer newId=null;
            if(items !=null && items.size()>0){
                for (FileItem item : items) {
                    if(item.isFormField()){//判断是否是表单域<form></form>
                        //获取字段名称
                        String fieldName = item.getFieldName();
                        if("id".equals(fieldName)){
                            String id = item.getString("UTF-8");
                            if(id!=null && !"null".equals(id)){
                                newId=Integer.valueOf(id);
                                newsDetail.setId(newId);
                            }
                        }else if("categoryId".equals(fieldName)){
                            String categoryId = item.getString("UTF-8");
                            newsDetail.setCategoryId(categoryId!=null && !"".equals(categoryId)? Integer.parseInt(categoryId) : 0);
                        }else if("title".equals(fieldName)){
                            String title = item.getString("UTF-8");
                            newsDetail.setTitle(title);
                        }else if("author".equals(fieldName)){
                            String author = item.getString("UTF-8");
                            newsDetail.setAuthor(author);
                        }else if("summary".equals(fieldName)){
                            String summary = item.getString("UTF-8");
                            newsDetail.setSummary(summary);
                        }else if("newscontent".equals(fieldName)){
                            String newscontent = item.getString("UTF-8");
                            newsDetail.setContent(newscontent);
                        }else {
                            String fileName = item.getName();
                            if(fileName!=null && !"".equals(fileName)){
                                String path="F:\\IJ\\SchoolWeb\\upload";
                                //String realPath = request.getSession().getServletContext().getRealPath("upload/");
                                // File 对象指向这个路径，File 是否存在
                                File file = new File(path+ "/"+System.currentTimeMillis()+ fileName);
                                try {
                                    item.write(file);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                String picPath="upload/"+fileName;
                                newsDetail.setPicpath(picPath);
                                response.sendRedirect("admin.jsp");
                            }
                        }
                    }
                }
            }
            if(newId!=null){
                NewsService newsDetailService=new NewsServiceImpl();
                Integer row = newsDetailService.updateNews(newsDetail);
                if(row>0){
                    request.getRequestDispatcher("/pages/admin/newsDetailList.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("/pages/admin/newsDetailCreateSimple.jsp").forward(request,response);
                }
            }else {
                NewsService newsDetailService=new NewsServiceImpl();
                int row = newsDetailService.addNews(newsDetail);
                if(row>0){
                    request.getRequestDispatcher("/pages/admin/newsDetailList.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("/pages/admin/newsDetailCreateSimple.jsp").forward(request,response);
                }
            }

        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
